<template>
  <div class="bg">
    <div class="form-content">
      <h2>Login</h2>
      <!-- 表单 -->
      <!-- 用户名 -->
      <n-form :model="model" ref="formRef" :rules="rules">
        <n-form-item path="username" label="用户名">
          <n-input v-model:value="model.username" @keydown.enter.prevent />
        </n-form-item>
        <!-- 密码 -->
        <n-form-item path="password" label="密码">
          <n-input
            v-model:value="model.password"
            type="password"
            @keydown.enter.prevent
          />
        </n-form-item>
        <!-- 登录按钮 -->
        <n-button
          @click="handleValidateButtonClick"
          round
          type="primary"
          class="button"
        >
          登录
        </n-button>
      </n-form>
      <!-- {{ JSON.stringify(model, 0, 2) }} -->
    </div>
  </div>
</template>
<script lang="ts">
import { ref } from "vue";
import {
  useMessage,
  NForm,
  NFormItem,
  NInput,
  NRow,
  NCol,
  NButton,
} from "naive-ui";
import { useRouter } from "vue-router";
export default {
  mounted(){
    localStorage.removeItem("key");
  },
  components: {
    NForm,
    NFormItem,
    NInput,
    NRow,
    NCol,
    NButton,
  },
  setup() {
    const formRef = ref(null as InstanceType<typeof NForm>);
    const message = useMessage();
    const modelRef = ref({
      username: null,
      password: null,
    })

    const router = useRouter();

    return {
      formRef,
      model: modelRef,
      rules: {
        username: [
          {
            required: true,
            validator(rule: any, value: any) {
              if (!value) {
                return new Error("用户名不能为空！");
              }
              // 此处判断缓存中是否存在该用户名
              let data = JSON.parse(localStorage.getItem("userDataSource")!);
              let uname = "";
              let index = -1;
              for (let i = 0; i < data.length; i++) {
                if (data[i].name === value) {
                  uname = value;
                  index = i
                }
              }
              localStorage.setItem("userIndex", index.toString())  
              if (!uname) {
                return new Error("用户名不存在");
              }
              return true;
            },
          },
        ],
        password: [
          {
            required: true,
            validator(rule: any, value: any) {
              if (!value) {
                return new Error("密码不能为空！");
              }
              // 此处判断缓存中是否存在该用户名的密码
              let data = JSON.parse(localStorage.getItem("userDataSource")!);
              let index = parseInt(localStorage.getItem("userIndex")!);
              console.log(index) 
              if (index !== -1 && data[index].password !== value) {
                  return new Error("密码错误");
              }

              return true;
            },
          },
        ],
      },
      handleValidateButtonClick(e: any) {
        e.preventDefault();
        formRef.value?.validate((errors: any) => {
          router.push("/welcome");
          if (!errors) {
            message.success("登录成功");
            localStorage.setItem("key", "key");
          } else {
            console.log(errors);
            message.error("登录失败");
          }
        });
      },
    };
  },
};
</script>
<style>
.bg {
  position: relative;
  height: 100%;
  width: 100%;
  background-image: linear-gradient(to top, #303030 0%, #030303 100%);
}
.form-content {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
  width: 450px;
  height: 300px;
  padding: 20px;
  background: #fff;
  border-radius: 25px;
  box-shadow: 0 0 10px #fff;
}
.form-content h2 {
  font-size: 30px;
  text-align: center;
}

.form-content .button {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 140px;
  height: 40px;
  transition: all 0.2s;
  font-size: 16px;
}
.form-content .button:hover {
  box-shadow: 0 0 10px green;
}
.form-content label {
  font-size: 15px !important;
}
</style>
